<?php

namespace app\admin\controller;

use app\common\controller\Backend;

use think\Controller;
use think\Request;
use think\Config;
use think\Log;
use think\Db;

/**
 * 
 *
 * @icon fa fa-circle-o
 */
class Borrowverify extends Backend
{
    
    /**
     * Borrow模型对象
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('Borrow');
        $typeList = array(
            '1'=>'新手标',
            '2'=>'车信贷',
            '3'=>'车抵贷',
            '4'=>'银盈宝',
            '5'=>'助业贷',
            '6'=>'黄金宝',
        );
        $pledge = array(
            '红本抵押'=>'红本抵押',
            '车辆抵押'=>'车辆抵押',
            '短期赎楼'=>'短期赎楼',
            '企业过桥'=>'企业过桥',
            '优质信贷'=>'优质信贷',
            '按揭房短拆'=>'按揭房短拆',
        );

        $actType = array(
            '0'=>'不选',
            '1'=>'加息标',
            '2'=>'热售标',
            '3'=>'活动标',
        );

        $activitylist = Db::name('activity')->field('id,title')->where('startTime <= '.time())->where('endTime >= '.time())->select();
        $this->view->assign("activitylist",$activitylist);
        $this->view->assign("actType",$actType);
        $this->view->assign("pledge",$pledge);
        $borrowTemp = Db::name('borrow_template')->field('id,tempname ,title')->select();
        $this->view->assign("typeList",$typeList);
        $this->view->assign("borrowTemp",$borrowTemp);

    }


    public function index()
    {
        //设置过滤方法
        //$this->request->filter(['strip_tags', 'htmlspecialchars']);
        if ($this->request->isAjax())
        {
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $total = $this->model
                ->where($where)->where('status',0)
                ->order($sort, $order)
                ->count();

            $list = $this->model
                ->where($where)->where('status',0)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();

            foreach($list as $key=>$val){
                if($val['isDay']==1){
                    $list[$key]['borrowtime'] = $val['time'].'天';
                }else{
                    $list[$key]['borrowtime'] = $val['time'].'个月';
                }
                switch($val['type']){
                    case '1': $type='新手标';break;
                    case '2': $type='车信贷';break;
                    case '3': $type='车抵贷';break;
                    case '4': $type='银盈宝';break;
                    case '5': $type='助业贷';break;
                    case '6': $type='黄金宝';break;
                    default:  $type='';
                }
                $list[$key]['type1']  =$type;
                $count = Db::name('borrow_tender')->where('borrowId',$val['borrowId'])->count();
                $list[$key]['tendercount'] = $count;
                $list[$key]['status1']  ='未发布';
            }

            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        return $this->view->fetch();
    }

    public function add()
    {
        if ($this->request->isPost())
        {
            $this->code = -1;
            $params = $this->request->post("row/a");
            if ($params)
            {
                foreach ($params as $k => &$v)
                {
                    $v = is_array($v) ? implode(',', $v) : $v;
                }
                try
                {
                    //是否采用模型验证
                    if ($this->modelValidate)
                    {
                        $name = basename(str_replace('\\', '/', get_class($this->model)));
                        $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : true) : $this->modelValidate;
                        $this->model->validate($validate);
                    }

                    $params['aprTotal'] = $params['aprBase'] + $params['aprAdd'];
                    $params['createTime'] = time();
                    $params['status'] = 0;
                    if($params['type']==1){
                        $params['maxMoney'] = 10000;
                    }
                    $result = $this->model->insertGetId($params);
                    if ($result !== false)
                    {
                        $vvvsss = $this->request->post("detail/a");
                        $vvvsss['borrowId'] = $result;
                        Db::name('borrow_detail')->insert($vvvsss);
                        $this->code = 1;
                    }
                    else
                    {
                        $this->msg = $this->model->getError();
                    }
                }
                catch (\think\exception\PDOException $e)
                {
                    $this->msg = $e->getMessage();
                }
            }
            else
            {
                $this->msg = __('Parameter %s can not be empty', '');
            }

            return;
        }
        return $this->view->fetch();
    }

    public function edit($ids = NULL)
    {
        $row = $this->model->get($ids);


        if (!$row)
            $this->error(__('No Results were found'));
        if ($this->request->isPost())
        {
            $this->code = -1;
            $params = $this->request->post("row/a");

            Log::record('postshuju='.json_encode($params));


            if ($params)
            {
                foreach ($params as $k => &$v)
                {
                    $v = is_array($v) ? implode(',', $v) : $v;
                }
                try
                {
                    //是否采用模型验证
                    if ($this->modelValidate)
                    {
                        $name = basename(str_replace('\\', '/', get_class($this->model)));
                        $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : true) : $this->modelValidate;
                        $row->validate($validate);
                    }
                    $result = $row->save($params);
                    if ($result !== false)
                    {
                        $vvvsss = $this->request->post("detail/a");
                        Db::name('borrow_detail')->where('borrowId',$ids)->update($vvvsss);
                        $this->code = 1;
                    }
                    else
                    {
                        $this->msg = $row->getError();
                    }
                }
                catch (think\exception\PDOException $e)
                {
                    $this->msg = $e->getMessage();
                }
            }
            else
            {
                $this->msg = __('Parameter %s can not be empty', '');
            }

            return;
        }

        $detail = Db::name('borrow_detail')->where('borrowId',$ids)->find();
        $row['detail'] = $detail;
        $this->view->assign("row", $row);
        return $this->view->fetch();
    }


    public function  shenhe(){
        $id = input('param.id');
        $url = Config::get('api_url');
        $data['borrowId'] = $id;
        $data['userId'] = $this->auth->id;

        Log::record('url='.$url.'/api/manager/publishBorrow');
        $result = postURL($url.'/api/manager/publishBorrow',$data);
        Log::record('发标审核返回:'.json_encode($result));
        $result = json_decode($result,true);
        if($result['code'] == 0){
            $this->code = 1;
        }else{
            $this->code = $result['code'];
            $this->msg = $result['msg'];
        }

        return;
    }


    public function choosetemp(){
        $id = input('param.id');
        $borrowtemp = Db::name('borrow_template')->where('id',$id)->find();
        if($borrowtemp){
             $borrowtemp['code'] = 1;
        }else{
            $borrowtemp['code'] = -1;
        }
        return $borrowtemp;
    }


}
